import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus";
import { useEnumStore } from "@/store/enumeration"
import {productLinkConfigDeleteDel,productLinkConfigPagePost} from "@/api/allProduct"
import useInstance from '@/hooks/useInstance';
import mittBus from '@/utils/mitt';

export const productLinkConfigListStore = defineStore("productLinkConfigListStoreId", () => {
    const {ProductLinkConfigStatusEnum} = useEnumStore()
    const { global } = useInstance();
    const addAndEditRef = ref<{ show: (title: string, row?: any) => void }>()
    const addEnvironmentRef = ref<{ show: (title: string, row?: any) => void }>()
    const addSceneRef = ref<{ show: (title: string, row?: any) => void }>()
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()

    const detailData = ref([
        {
            type: "text",
            prop: "channelKey",
            name: "渠道Key",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "productLinkConfigList",
            propsList: [

                {
                    label: '序号',
                    prop: 'id',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    label: '产品名称',
                    prop: 'productName',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '产品编码',
                    prop: 'productCode',
                    type: 'normal',
                    minWidth: '120px',
                    hidden: true,
                },
                {
                    label: '产品大类',
                    prop: 'productCategoryName',
                    type: 'normal',
                    minWidth: '150px',
                    hidden: true,
                },
                // {
                //     label: '渠道编码',
                //     prop: 'channelCode',
                //     type: 'normal',
                //     minWidth: '280px',
                //     hidden: true,
                // },
                {
                    label: '环境',
                    prop: 'environmentName',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '场景',
                    prop: 'scenarioName',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '产品链接',
                    prop: 'productLink',
                    type: 'copy',
                    minWidth: '350px',
                    hidden: true,
                },
                {
                    label: '状态',
                    prop: 'status',
                    type: 'text',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    label: '图片',
                    prop: 'attachmentUri',
                    type: 'custom',
                    slotName:'attachmentUri',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    label: '备注',
                    prop: 'remarks',
                    type: 'text',
                    minWidth: '100px',
                    hidden: true,
                },
            
                {
                    type: 'handle',
                    label: '操作',
                    width: '150px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })

    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            id:null, // id
            productCode:null,// 产品编码
            productCategoryCode:null,// 产品大类
            schemeCode:null,// 权益方案编码
            // channelCode:null,// 渠道编码
            environment:null,// 环境
            scenario:null,// 场景
            productLink:null,// 产品链接
            status:null,// 状态
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })
    const getDate1 = async (type: string) => {
        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                // @ts-ignore
                listSearch.condition[it] = null
            })
        }

        try {
            const res = await productLinkConfigPagePost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {                    
                    return {
                        ...item,
                        environmentName:item.environment + ' - ' + item.environmentName,
                        scenarioName:item.scenario + ' - ' + item.scenarioName,
                        status:ProductLinkConfigStatusEnum[item.status - 1]?.value
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }finally{
            mittBus.emit('scrollUpdate')
        }
    }
    const detailBtn = async (row: any) => {
        try {
            detailRef.value?.show('edit', row)
        } catch (error) {
            console.log(error);
        }
    }
    const handPage = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }
    const delBtn = async (row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.productName}`)
        if (flag) {
            const res = await productLinkConfigDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                getDate1('search')
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }
    const AddEdit = async (type: string, row?: any) => {
        if (type == 'add') {
            addAndEditRef.value?.show(type)
        } else {
        //   转化一下

            addAndEditRef.value?.show(type, row)
        }
    }
    const addEnvironmentBtn = async (type: string, row?: any) => {
        if (type == 'add') {
            addEnvironmentRef.value?.show(type)
        } 
    }
    const addSceneBtn = async (type: string, row?: any) => {
        if (type == 'add') {
            addSceneRef.value?.show(type)
        }
    }
    return {
        detailData, tableProp, listSearch, pageParams, addAndEditRef, addSceneRef, addEnvironmentRef,detailRef,
        getDate1, detailBtn, handPage, delBtn, AddEdit,addEnvironmentBtn,addSceneBtn
    }
})